# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  def loggined_by_valleyer?
    Thread.current[:current_valleyer_id]
  end

  def loggined_by?(valleyer_or_id)
    valleyer_or_id.try(:id) == current_valleyer_id || valleyer_or_id == current_valleyer_id
  end

  def current_valleyer
    if current_valleyer_id == nil
      nil
    else
      Valleyer.find(current_valleyer_id)
    end
  end

  def current_valleyer_id
    session[:current_valleyer_id]
  end

  #当前登录者是否admin
  def loggined_by_admin?
    current_valleyer.nick_name.eql?("admin")
  end

  #翻页器
  def page_turner(cmd, model)
    if model.total_pages > 1
      "<div id='page'>" +
        previous_page(cmd, model) +
        "<div class='button'>" +
          page_number(cmd, model) +
        "</div>" +
        next_page(cmd, model) +
      "</div>"
    end
  end

  #上一页
  def previous_page(cmd, model)
    if model.current_page == 1
      link_to_function image_tag("valleyer/fore.jpg"),
        "page_tumer('#{cmd}', #{1})",
        :id => :fore
    else
      link_to_function image_tag("valleyer/fore.jpg"),
        "page_tumer('#{cmd}', #{(model.current_page - 1)})",
        :id => :fore
    end
  end

  #下一页
  def next_page(cmd, model)
    if model.current_page == model.total_pages
      link_to_function image_tag("valleyer/rear.jpg"),
        "page_tumer('#{cmd}', #{(model.total_pages)})",
        :id => :rear
    else
      link_to_function image_tag("valleyer/rear.jpg"),
        "page_tumer('#{cmd}', #{(model.current_page + 1)})",
        :id => :rear
    end
  end

  #页码
  def page_number(cmd, model)
    #总页数少于5页
    bit_page_count = 2..(model.total_pages - 1)
    #总页数大于5页
    #当前页<=3
    less_than_four_page_count = 2..5
    #当前页>=倒数第3页
    greater_than_antepenultimate_page_count = (model.total_pages - 4)..(model.total_pages - 1)
    #当前页在中间
    large_page_count = (model.current_page - 2)..(model.current_page + 2)
    #第一页始终显示
    body = create_page(cmd, model, 1)
    #如果当前页小于4 显示2-5页
    if model.total_pages <= 5
      bit_page_count.each do |number|
        body += create_page(cmd, model, number)
      end
    else
      if model.current_page <= 3
        less_than_four_page_count.each do |number|
          body += create_page(cmd, model, number)
        end
        body = body + "<div class='sign'>…</div>"
      else if model.current_page >= model.total_pages - 2
          body = body + "<div class='sign'>…</div>"
          greater_than_antepenultimate_page_count.each do |number|
            body += create_page(cmd, model, number)
          end
        else
          body = body + "<div class='sign'>…</div>"
          large_page_count.each do |number|
            body += create_page(cmd, model, number)
          end
          body = body + "<div class='sign'>…</div>"
        end
      end
    end
    #最后一页始终显示
    #如果只有一页就不现实了
    body += create_page(cmd, model, model.total_pages)
    return body
  end

  #页码的css格式
  #当前页不同
  def create_page(cmd, model, number)
    if number == model.current_page
      link_to_function number,
        "page_tumer('#{cmd}', #{number})",
        :class => :num_h
    else
      link_to_function number,
        "page_tumer('#{cmd}', #{number})",
        :class => :num_l
    end
  end
end
